﻿namespace Hidistro.SqlDal.Orders
{
    using Hidistro.Core;
    using Hidistro.Core.Entities;
    using Hidistro.Entities.Orders;
    using Microsoft.Practices.EnterpriseLibrary.Data;
    using System;
    using System.Data;
    using System.Data.Common;
    using System.Runtime.InteropServices;
    using System.Text;

    public class ReturnDao
    {
        private Database database = DatabaseFactory.CreateDatabase();

        public bool ApplyForReturn(string orderId, string remark, int refundType)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("UPDATE Hishop_Orders SET OrderStatus = @OrderStatus WHERE OrderId = @OrderId;");
            builder.Append(" insert into Hishop_OrderReturns(OrderId,ApplyForTime,Comments,HandleStatus,RefundType,RefundMoney) values(@OrderId,@ApplyForTime,@Comments,0,@RefundType,0);");
            DbCommand sqlStringCommand = this.database.GetSqlStringCommand(builder.ToString());
            this.database.AddInParameter(sqlStringCommand, "OrderStatus", DbType.Int32, 7);
            this.database.AddInParameter(sqlStringCommand, "OrderId", DbType.String, orderId);
            this.database.AddInParameter(sqlStringCommand, "ApplyForTime", DbType.DateTime, DateTime.Now);
            this.database.AddInParameter(sqlStringCommand, "Comments", DbType.String, remark);
            this.database.AddInParameter(sqlStringCommand, "RefundType", DbType.Int32, refundType);
            return (this.database.ExecuteNonQuery(sqlStringCommand) > 0);
        }

        public bool CanReturn(string orderId)
        {
            DbCommand sqlStringCommand = this.database.GetSqlStringCommand("select OrderId,HandleStatus from Hishop_OrderReturns where OrderId = @OrderId");
            this.database.AddInParameter(sqlStringCommand, "OrderId", DbType.String, orderId);
            bool flag = false;
            using (IDataReader reader = this.database.ExecuteReader(sqlStringCommand))
            {
                if (reader.Read())
                {
                    if (Convert.ToInt32(reader["HandleStatus"]) == 2)
                    {
                        flag = true;
                    }
                    return flag;
                }
                return true;
            }
        }

        public bool CheckReturn(string orderId, string Operator, decimal refundMoney, string adminRemark, int refundType, bool accept)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("UPDATE Hishop_Orders SET OrderStatus = @OrderStatus WHERE OrderId = @OrderId;");
            builder.Append(" update Hishop_OrderReturns set Operator=@Operator, AdminRemark=@AdminRemark,HandleStatus=@HandleStatus,HandleTime=@HandleTime,RefundMoney=@RefundMoney where HandleStatus=0 and OrderId = @OrderId;");
            if ((refundType == 1) && accept)
            {
                builder.Append(" insert into Hishop_BalanceDetails(UserId,UserName,TradeDate,TradeType,Income");
                builder.AppendFormat(",Balance,Remark) select UserId,Username,getdate() as TradeDate,{0} as TradeType,", 7);
                builder.Append("@RefundMoney as Income,isnull((select top 1 Balance from Hishop_BalanceDetails b");
                builder.AppendFormat(" where b.UserId=a.UserId order by JournalNumber desc),0)+@RefundMoney as Balance,'订单{0}退货' as Remark ", orderId);
                builder.Append("from Hishop_Orders a where OrderId = @OrderId;");
            }
            DbCommand sqlStringCommand = this.database.GetSqlStringCommand(builder.ToString());
            if (accept)
            {
                this.database.AddInParameter(sqlStringCommand, "OrderStatus", DbType.Int32, 10);
                this.database.AddInParameter(sqlStringCommand, "HandleStatus", DbType.Int32, 1);
            }
            else
            {
                this.database.AddInParameter(sqlStringCommand, "OrderStatus", DbType.Int32, 3);
                this.database.AddInParameter(sqlStringCommand, "HandleStatus", DbType.Int32, 2);
            }
            this.database.AddInParameter(sqlStringCommand, "OrderId", DbType.String, orderId);
            this.database.AddInParameter(sqlStringCommand, "Operator", DbType.String, Operator);
            this.database.AddInParameter(sqlStringCommand, "AdminRemark", DbType.String, adminRemark);
            this.database.AddInParameter(sqlStringCommand, "HandleTime", DbType.DateTime, DateTime.Now);
            this.database.AddInParameter(sqlStringCommand, "RefundMoney", DbType.Decimal, refundMoney);
            return (this.database.ExecuteNonQuery(sqlStringCommand) > 0);
        }

        public bool DelReturnsApply(int returnsId)
        {
            string query = string.Format("DELETE FROM Hishop_OrderReturns WHERE ReturnsId={0} and HandleStatus >0 ", returnsId);
            DbCommand sqlStringCommand = this.database.GetSqlStringCommand(query);
            return (this.database.ExecuteNonQuery(sqlStringCommand) > 0);
        }

        public decimal GetRefundMoney(OrderInfo order, out decimal refundMoney)
        {
            decimal num = 0M;
            string query = string.Format("SELECT RefundMoney FROM dbo.Hishop_OrderReturns WHERE HandleStatus=1 AND OrderId={0}", order.OrderId);
            DbCommand sqlStringCommand = this.database.GetSqlStringCommand(query);
            num = Convert.ToDecimal(this.database.ExecuteScalar(sqlStringCommand));
            return (refundMoney = num);
        }

        public void GetRefundTypeFromReturn(string orderId, out int refundType, out string remark)
        {
            refundType = 0;
            remark = "";
            string query = "select RefundType,Comments from Hishop_OrderReturns where HandleStatus=0 and OrderId='" + orderId + "'";
            DbCommand sqlStringCommand = this.database.GetSqlStringCommand(query);
            IDataReader reader = this.database.ExecuteReader(sqlStringCommand);
            if (reader.Read())
            {
                refundType = (int) reader["RefundType"];
                remark = (string) reader["Comments"];
            }
        }

        public DbQueryResult GetReturnsApplys(ReturnsApplyQuery query)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append(" 1=1");
            if (!string.IsNullOrEmpty(query.OrderId))
            {
                builder.AppendFormat(" and OrderId = '{0}'", query.OrderId);
            }
            if (query.HandleStatus.HasValue)
            {
                builder.AppendFormat(" AND HandleStatus = {0}", query.HandleStatus);
            }
            if (query.UserId.HasValue)
            {
                builder.AppendFormat(" AND UserId = {0}", query.UserId.Value);
            }
            return DataHelper.PagingByRownumber(query.PageIndex, query.PageSize, query.SortBy, query.SortOrder, query.IsCount, "vw_Hishop_OrderReturns", "ReturnsId", builder.ToString(), "*");
        }
    }
}

